﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;

using EHTWebsiteProject.Code.BLL;
using EHTWebsiteProject.Code.Entity;
using EHTWebsiteProject.Code.Utility;
namespace EHTWebsiteProject
{
    public partial class ViewCart : System.Web.UI.Page
    {
        // public String checkOutUrl = WebConfigurationManager.AppSettings["CheckOutUrl"].ToString();
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(ViewCart));
            if (!IsPostBack)
            {
                BindData();

            }
        }

        protected void BindData()
        {
            try
            {
                gvShoppingCart.DataSource = ShoppingCart.Instance.Items;
                gvShoppingCart.DataBind();
                btnPayment.Visible = (ShoppingCart.Instance.Items.Count > 0);
                btnUpdateCart.Visible = (ShoppingCart.Instance.Items.Count > 0);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnUpdateCart_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvShoppingCart.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    try
                    {
                        int ProductID = Convert.ToInt32(gvShoppingCart.DataKeys[row.RowIndex].Value);
                        int quantity = int.Parse(((TextBox)row.Cells[1].FindControl("txtQuantity")).Text);
                        ShoppingCart.Instance.SetItemQuantity(ProductID, quantity);
                    }
                    catch (FormatException ex)
                    {
                        throw ex;
                    }
                }

            }
            Response.Redirect("Vis-handlekurv.html");
        }

        protected void btnPayment_Click(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session["Username"] == null)
            {
                Response.Redirect("LoggInn.html");
            }
            else
            {
                Response.Redirect("FraktPalegg.html");
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("Hjem.html");
        }

        protected void gvShoppingCart_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Remove")
            {
                int SanPhamID = Convert.ToInt32(e.CommandArgument);
                ShoppingCart.Instance.RemoveItem(SanPhamID);
            }

            // We now have to re-setup the data so that the GridView doesn't keep
            // displaying the old data
            BindData();
        }

        protected void gvShoppingCart_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[3].Text = "Total: " + ShoppingCart.Instance.GetSubTotal().ToString("C");
            }
        }

        [AjaxPro.AjaxMethod]
        public string DelShopCart(int SanPhamID)
        {
            try
            {
                ShoppingCart.Instance.RemoveItem(SanPhamID);
                return "Delete Success:";
            }
            catch (Exception ex)
            {
                return "Error" + ex.Message;
            }
        }
    }
}